package worker.control;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import worker.model.service.HistoryService;
import worker.model.vo.HistoryVO;
import worker.model.vo.ListVO;

public class HistoryController extends MultiActionController {
	private HistoryService historyService;
	
	public void setHistoryService(HistoryService historyService) {
		this.historyService = historyService;
	}
	
	//위 메뉴에서 history 클릭시 도는 콘트롤러
	public ModelAndView main(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		HashMap map=new HashMap();
		String p=request.getParameter("pageNo");
		if(p==null||p.equals("")){
			p="1";
		}
		int pageNo=Integer.parseInt(p);
		ListVO lvo=historyService.totalInfo(pageNo);
		map.put("lvo", lvo);
		System.out.println("history정보 콘트롤러: "+map);
		return new ModelAndView("history.history_main","map",map);
	}
	
	//시리얼번호로 검색시 콘트롤러
	public ModelAndView searchSerialList(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		HashMap map=new HashMap();
		String serial_no=request.getParameter("serial_no");
		String p=request.getParameter("pageNo");
		if(p==null||p.equals("")){
			p="1";
		}
		int pageNo=Integer.parseInt(p.trim());
		map.put("serial_no", serial_no);
		map.put("pageNo", pageNo);
		ListVO lvo=historyService.getSerialList(map);
		if(lvo.getList().size()==0){
			System.out.println("시리얼번호로 검색시 콘트롤러: "+lvo);
			return new ModelAndView("JsonView","msg","N");
		}
		System.out.println("시리얼번호로 검색시 콘트롤러: "+lvo);
		return new ModelAndView("JsonView","lvo",lvo);
	}
	
	//워크오더로 검색시 콘트롤러
	public ModelAndView searchWorkOderList(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		HashMap map=new HashMap();
		HashMap map2=new HashMap();
		String work_order_id=request.getParameter("work_order_id");
		String operation_id=request.getParameter("operation_id");
		String defect_id=request.getParameter("defect_id");
		String p=request.getParameter("pageNo");
		if(p==null||p.equals("")){
			p="1";
		}
		int pageNo=Integer.parseInt(p.trim());
		map2.put("work_order_id", work_order_id);
		map2.put("operation_id", operation_id);
		map2.put("defect_id", defect_id);
		map2.put("pageNo", pageNo);
		ListVO lvo=historyService.searchWorkOderList(map2);
		if(lvo.getList().size()==0){
			System.out.println("워크오더로 검색시 콘트롤러: "+map);
			return new ModelAndView("JsonView","msg","N");
		}
		map.put("lvo", lvo);
		System.out.println("워크오더로 검색시 콘트롤러: "+map);
		return new ModelAndView("JsonView","map",map);
	}
	//디펙트로 검색시 콘트롤러
	public ModelAndView searchDefectList(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		HashMap map=new HashMap();
		HashMap map2=new HashMap();
		String defect_id=request.getParameter("defect_id");
		System.out.println(defect_id);
		String p=request.getParameter("pageNo");
		if(p==null||p.equals("")){
			p="1";
		}
		int pageNo=Integer.parseInt(p.trim());
		map2.put("defect_id", defect_id);
		map2.put("pageNo", pageNo);
		ListVO lvo=historyService.searchDefectList(map2);
		if(lvo.getList().size()==0){
			System.out.println("디펙트로 검색시 콘트롤러: "+map);
			return new ModelAndView("JsonView","msg","N");
		}
		map.put("lvo", lvo);
		System.out.println("디펙트로 검색시 콘트롤러: "+map);
		return new ModelAndView("JsonView","map",map);
	}
}






